/*
* @Author: Administrator
* @Date:   2017-12-23 15:54:49
* @Last Modified by:   Administrator
* @Last Modified time: 2017-12-23 16:03:35
*/
import { observable, action } from 'mobx'
import { get } from '../util/comm'
import { newsListUrl } from '../util/constant';

const objToStrMap = (obj) => {
  const strMap = new Map();
  for (let k of Object.keys(obj)) { //eslint-disable-line
    strMap.set(k, obj[k]);
  }
  return strMap;
}
class NewsList {
  // 列表模块新闻的数据
  @observable newsListData
  // 图片模块新闻的数据
  @observable newsImageData
  // 新闻分类页面的新闻数据
  @observable newsPageData
  // slinder 新闻的数据
  @observable newsSliderData
  constructor(initialState) {
    if (initialState && initialState.newsList) {
      if (initialState.newsList.newsListData) {
        this.newsListData = objToStrMap(initialState.newsList.newsListData)
      } else {
        this.newsListData = new Map()
      }
      if (initialState.newsList.newsImageData) {
        this.newsImageData = objToStrMap(initialState.newsList.newsImageData)
      } else {
        this.newsImageData = new Map()
      }
      if (initialState.newsList.newsPageData) {
        this.newsPageData = objToStrMap(initialState.newsList.newsPageData)
      } else {
        this.newsPageData = new Map()
      }
      if (initialState.newsList.newsSliderData) {
        this.newsSliderData = objToStrMap(initialState.newsList.newsSliderData)
      } else {
        this.newsSliderData = new Map()
      }
    } else {
      this.newsPageData = new Map()
      this.newsListData = new Map()
      this.newsImageData = new Map()
      this.newsSliderData = new Map()
    }
  }
  @action getListNews(channel, num, page = 1) {
    return new Promise((resolve, reject) => {
      const key = decodeURIComponent(channel)
      if (!this.newsListData.get(key)) {
        const baseUrl = newsListUrl;
        const params = {
          channel,
          page,
          num,
        }
        get(baseUrl, params).then((result) => {
          if (result.status === 200 && result.data.code === 1) {
            this.newsListData.set(key, result.data)
            resolve(result.data.data)
          } else {
            reject(result.data.message)
          }
        }).catch((e) => { reject(e) })
      } else {
        resolve()
      }
    })
  }
  @action getImageNews(channel, num, page = 1) {
    const key = decodeURIComponent(channel)
    return new Promise((resolve, reject) => {
      if (!this.newsImageData.get(key)) {
        const baseUrl = newsListUrl;
        const params = {
          channel,
          page,
          num,
        }
        get(baseUrl, params).then((result) => {
          if (result.status === 200 && result.data.code === 1) {
            this.newsImageData.set(key, result.data)
            resolve(result.data.data)
          } else {
            reject(result.data.message)
          }
        }).catch((e) => {
          reject(e)
        })
      } else {
        resolve()
      }
    })
  }
  @action getPageNews(channel, num, page = 1) {
    return new Promise((resolve, reject) => {
      if (!this.newsPageData.get(`${channel}${page}`)) {
        const baseUrl = newsListUrl;
        const params = {
          channel,
          page,
          num,
        }
        const key = `${decodeURIComponent(channel)}${page}`
        get(baseUrl, params).then((result) => {
          if (result.status === 200 && result.data.code === 1) {
            this.newsPageData.set(key, result.data)
            resolve()
          } else {
            reject(result.data.message)
          }
        }).catch((e) => {
          reject(e)
        })
      } else {
        resolve()
      }
    })
  }
  @action getSliderNews(channel, num, page = 1) {
    const key = decodeURIComponent(channel)
    return new Promise((resolve, reject) => {
      if (!this.newsSliderData.get(key)) {
        const baseUrl = newsListUrl;
        const params = {
          channel,
          page,
          num,
        }
        get(baseUrl, params).then((result) => {
          if (result.status === 200 && result.data.code === 1) {
            this.newsSliderData.set(key, result.data)
            resolve()
          } else {
            reject(result.data.message)
          }
        }).catch(e => reject(e))
      } else {
        resolve()
      }
    })
  }
  @action deleteListNews(key) {
    try {
      this.newsListData.delete(key)
    } catch (e) {
      throw e
    }
  }
  @action deleteImageNews(key) {
    try {
      this.newsImageData.delete(key)
    } catch (e) {
      throw e
    }
  }
  @action deletePageNews(key) {
    try {
      this.newsPageData.delete(key)
    } catch (e) {
      throw e
    }
  }
  @action deleteSliderNews(key) {
    try {
      this.newsSliderData.delete(key)
    } catch (e) {
      throw e
    }
  }
  @action combineNewsData(dataList, newsDataList) {
    dataList = dataList.concat(newsDataList)
  }
}
// autorun(() => {
//   console.log(111);
// })
export default NewsList
